package com.powersi.hygeia.framework.jdbclogger.util;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TreeMap;
import java.util.Vector;

/**
 * @author Martin Marinschek (latest modification by $Author: catalean $)
 * @version $Revision: 83 $ $Date: 2007-07-07 23:00:58 +0200 (Sat, 07 Jul 2007) $
 */
public class Tokenizer {
    public static char NO_COMMENT_CHARACTER = '\0';
    private Vector _indexToTokenEntries;
    private int _currentIndex;

    public Tokenizer(String str, String delimiter) {
        this(str, new String[] { delimiter });
    }

    public Tokenizer(String str, String delimiters[]) {
        this(str, delimiters, false, false, NO_COMMENT_CHARACTER, NO_COMMENT_CHARACTER);
    }

    public Tokenizer(String str, String delimiters[], boolean returnDelimiters, boolean returnEmptyStrings, char commentStartCharacter, char commentEndCharacter) {
        _currentIndex = 0;
        StringBuffer buf = new StringBuffer(str);
        TreeMap indexToDelimiterMap = new TreeMap();
        for (int i = 0; i < delimiters.length; i++)
            getDelimiterIndizes(buf, delimiters[i], commentStartCharacter, commentEndCharacter, indexToDelimiterMap);

        TreeMap indexToTokenMap = new TreeMap();
        getTokenIndizes(buf, indexToDelimiterMap, indexToTokenMap, returnEmptyStrings);
        if (returnDelimiters)
            indexToTokenMap.putAll(indexToDelimiterMap);
        _indexToTokenEntries = new Vector();
        _indexToTokenEntries.addAll(indexToTokenMap.entrySet());
    }

    public boolean hasMoreTokens() {
        return _currentIndex < _indexToTokenEntries.size();
    }

    public String nextToken() {
        if (_currentIndex >= _indexToTokenEntries.size()) {
            throw new NoSuchElementException();
        } else {
            java.util.Map.Entry entry = (java.util.Map.Entry) _indexToTokenEntries.get(_currentIndex);
            String returnString = (String) entry.getValue();
            _currentIndex++;
            return returnString;
        }
    }

    public int countTokens() {
        return _indexToTokenEntries.size() - _currentIndex;
    }

    private static void getTokenIndizes(StringBuffer buf, TreeMap indexToDelimiterMap, TreeMap tokenMap, boolean returnEmptyStrings) {
        Iterator it = indexToDelimiterMap.entrySet().iterator();
        int lastStopIndex;
        int currentStopIndex;
        String delimiterString;
        for (lastStopIndex = 0; it.hasNext(); lastStopIndex = currentStopIndex + delimiterString.length()) {
            java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
            currentStopIndex = (int) ((Double) entry.getKey()).floatValue();
            putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, currentStopIndex);
            delimiterString = (String) entry.getValue();
        }

        putTokenIfNecessary(buf, tokenMap, returnEmptyStrings, lastStopIndex, buf.length());
    }

    private static void putTokenIfNecessary(StringBuffer buf, TreeMap tokenMap, boolean returnEmptyStrings, int lastStopIndex, int currentStopIndex) {
        String returnString = buf.substring(lastStopIndex, currentStopIndex);
        if (returnString.trim().equals("")) {
            if (returnEmptyStrings)
                tokenMap.put(new Double((double) lastStopIndex - 0.5D), returnString);
        } else {
            tokenMap.put(new Double(lastStopIndex), returnString);
        }
    }

    private static void getDelimiterIndizes(StringBuffer buf, String delimiter, char commentStartCharacter, char commentEndCharacter, TreeMap indexToDelimiterMap) {
        int delimiterIndex = 0;
        boolean isComment = false;
        if (commentEndCharacter == NO_COMMENT_CHARACTER)
            commentEndCharacter = commentStartCharacter;
        for (int i = 0; i < buf.length(); i++) {
            if (buf.charAt(i) == commentStartCharacter && !isComment || buf.charAt(i) == commentEndCharacter && isComment) {
                isComment = !isComment;
                delimiterIndex = 0;
                continue;
            }
            if (isComment)
                continue;
            if (buf.charAt(i) == delimiter.charAt(delimiterIndex)) {
                if (++delimiterIndex == delimiter.length()) {
                    int delimiterStartIndex = (i - delimiterIndex) + 1;
                    indexToDelimiterMap.put(new Double(delimiterStartIndex), delimiter);
                    delimiterIndex = 0;
                }
            } else {
                delimiterIndex = 0;
            }
        }
    }
}
